#include <stdlib.h>
#include <stdio.h>
#include "../include/pinyinime.h"
#include <cassert>
#include <memory.h>
#include <locale.h>
#include <iostream>
#include <codecvt>

using namespace ime_pinyin;

int main(int argc, char* argv[])
{
	setlocale(LC_ALL, "");
	
	char const *szSysDict = "./dict_pinyin.dat";
	char const *szUserDict = "";
	if (argc >= 2) {
		szSysDict = argv[1];
	}

	bool ret = im_open_decoder(szSysDict, szUserDict);  // 加载
	assert(ret);
	im_set_max_lens(32, 16);
	char szLine[256];

	while (true) {
		printf("\n >");
		fgets(szLine, 256, stdin);
		for(auto i=0; i<strlen(szLine); i++){
			if (szLine[i] == '\n')
				szLine[i] = '\0';
		}
		if (strlen(szLine) == 0)
			break;
		
		im_reset_search();
		size_t nr = im_search(szLine, strlen(szLine)); // 查询
		size_t size = 0;
		printf("输入串：%s\n候选串：", im_get_sps_str(&size));  // 返回拼音串
		char16 str[64] = { 0 };
		for (auto i = 0; i < nr; i++)
		{
			im_get_candidate(i, str, 32);         // 获取查询候选
			std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
			std::string strCand = convert.to_bytes((char16_t*)str);
			std::cout << i << "." << strCand << " ";
		}
		printf(" 共%lu个候选\n", nr);
	}
	im_close_decoder();                 // 关闭
	return 0;
}
